Python scrapes data from website just fine yet Flask is giving me a hard time doing the same thing

by: erikvm, 8 years ago

Last edited: 8 years ago

Hello,

I am trying to get all the headlines from http://www.dailymail.co.uk/home/latest/index.html for my flask app.

The following code works just fine and returns the desired result when running it in the Python IDE:


import urllib.request
from bs4 import BeautifulSoup
import requests

page = requests.get("http://www.dailymail.co.uk/home/latest/index.html","lxml")
soup = BeautifulSoup(page.content)

gData = soup.find_all("a",{"class": "title"})
for item in gData:
print (item.text)




Yet, when I try to do the "exact" same thing for my flask app. I'm just getting a big error message and it just won't.

Here's what my flask code looks like:

from flask import Flask, render_template, jsonify
from lxml import html
import urlopen, urllib
import requests
from bs4 import BeautifulSoup

app = Flask(__name__)

@app.route("/")
def index():    
    result = requests.get("http://api.wunderground.com/api/f0d1727c1475a44a/conditions/q/SE/Stockholm.json")
    weather = result.json()
    location = weather["current_observation"]["display_location"]["full"]
    temperature = int(weather["current_observation"]["temp_c"])
    combinedString = str(temperature) +" degrees in " + str(location) #string needed - Flask can only return 1 value
    return combinedString

@app.route("/news")
def news():
    page = requests.get("http://www.dailymail.co.uk/home/latest/index.html","lxml")
    soup = BeautifulSoup(page.content)
    gData = soup.find_all("a",{"class": "title"})
    return gData

if __name__ == "__main__":
    app.run(debug=True)


When I go to my localhost, I get a big error message with a type error at the end saying "TypeError: 'ResultSet' object is not callable"

What is wrong with my code? How can I get the headlines to be displayed on the localhost?

Thanks a lot





You must be logged in to post. Please login or register an account.



Have you tried just simply printing the source code that you grab? So before you actually use beautiful soup and all, just print out page.content, and see what the content is. If it's different, that's a start to debugging. If not, I am not really sure what more it would be. Since you say you are running from localhost, I cannot imagine what would be different though.

-Harrison 8 years ago

You must be logged in to post. Please login or register an account.